<template>
	<view class="content">
		<!-- 头部状态栏 -->
		<view class="todo-header" v-if="list.length !== 0">
			<!-- 状态栏的左侧 -->
			<view class="todo-header__left">
				<text class="active-text">{{text}}</text>
				<text>{{listData.length}}条</text>
			</view>
			<!-- 状态栏的右侧 -->
			<view class="todo-header__right">
				<view class="todo-header__right-item" :class="{'active-tab': activeIndex === 0}" @click="tab(0)">全部</view>
				<view class="todo-header__right-item" :class="{'active-tab': activeIndex === 1}" @click="tab(1)">待办</view>
				<view class="todo-header__right-item" :class="{'active-tab': activeIndex === 2}" @click="tab(2)">已完成</view>
			</view>
		</view>
		<!-- 没有数据 -->
		<view class="default" v-if="list.length === 0">
			<view class="image-default">
				<image src="../../static/default.png" mode="aspectFit"></image>
			</view>
			<view class="default-info">
				<view class="default-info__text">您还没有创建任何待办事项</view>
				<view class="default-info__text">点击下方+创建一个吧!</view>
			</view>
		</view>
		<!-- 有数据   具体的事项的list -->
		<view class="todo-content" v-else>
			<!-- todo--finish -->
			<view class="todo-list" :class="{'todo--finish': item.checked}" v-for="(item, index) in listData" :key="index" @click="finish(item.id)">
				<view class="todo-list__checkbox">
					<view class="checkbox"></view>
				</view>
				<view class="todo-list__content">
					{{item.content}}
				</view>
			</view>
		</view>
		<!-- 添加待办事项的加号 + 按钮 (创建按钮)-->
		<view class="create-todo" @click="create">
			<text class="iconfont icon-add" :class="{'create-todo-active': textShow}"></text>
		</view>
		<!-- 添加待办事项的弹出输入框 -->
		<view class="create-content" :class="{'create--show': textShow}" v-if="active">
			<view class="create-content-box">
				<!-- input输入 -->
				<view class="create-input">
					<input type="text" v-model="value" placeholder="请输入您要创建的TODO" />
				</view>
				<!-- 发布创建 -->
				<view class="create-button" @click="add">创建</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: '', // 表单输入的内容
				list: [],  // 数据
				active: false, // +加号创建按钮
				activeIndex: 0, // tab切换控制
				text: '全部',
				textShow: false
			}
		},
		onLoad() {

		},
		computed: {
			listData() {
				let list = JSON.parse(JSON.stringify(this.list))
				let newList = []
				// 点击 全部
				if (this.activeIndex === 0) {
					this.text = '全部'
					return list
				}
				// 点击 待办事项
				if (this.activeIndex === 1) {
					// checked = false
					list.forEach(item => {
						if (!item.checked) {
							newList.push(item)
						}
					})
					this.text = '待办'
					return newList
				}
				// 点击 已完成
				if (this.activeIndex === 2) {
					// checked = true
					list.forEach(item => {
						if (item.checked) {
							newList.push(item)
						}
					})
					this.text = '已完成'
					return newList
				}
				return []
			}
		},
		methods: {
			/**
			 * 打开输入框
			 */
			create() {
				if (this.active) {
					this.close()
				} else {
					this.open()
				}
			},
			/**
			 * 打开动画
			 */
			open() {
				this.active = true
				this.$nextTick(() => {
					setTimeout(() => {
						this.textShow = true
					}, 50)
				})
			},
			/**
			 * 关闭动画
			 */
			close() {
				this.textShow = false
				this.$nextTick(() => {
					setTimeout(() => {
						this.active = false
					}, 350)
				})
			},
			/**
			 * 添加发布
			 */
			add() {
				if (this.value === '') {
					uni.showToast({
						icon: 'none',
						title: '请输入内容'
					})
					return
				}
				this.list.unshift({
					id: 'id' + new Date().getTime(),
					content: this.value,
					checked: false
				})
				this.value = ''
				this.close()
			},
			/**
			 * 点击列表除非
			 */
			finish(id) {
				let index = this.list.findIndex((item) => item.id === id)
				this.list[index].checked = !this.list[index].checked
			},
			/**
			 * tab切换
			 */
			tab(index) {
				this.activeIndex = index
			}
		}
	}
</script>

<style>
	/* 引入公共css */
	@import '../../common/icon.css';
	/* 头部header和tab切换 */
	.todo-header {
		width: 100%;
		box-sizing: border-box;
		position: fixed;
		top: 0;
		left: 0;
		display: flex;
		align-items: center;
		padding: 0 15px;
		font-size: 12px;
		color: #333333;
		height: 45px;
		box-shadow:  -1px 1px 5px 0px rgba(0,0,0,0.1);
		background: #FFFFFF;
		z-index: 10;
	}
	.todo-header__left {
		width: 100%;
	}
	.active-text {
		font-size:  14px;
		color: #279abf;
		padding-right: 10px;
	}
	.todo-header__right {
		flex-shrink: 0;
		display: flex;
	}
	.todo-header__right-item {
		padding: 0 5px;
	}
	.active-tab {
		color: #279abf;
	}
	
	/* 事项列表 */
	.todo-content {
		position: relative;
		padding-top: 50px;
		padding-bottom: 100px;
	}
	.todo-list {
		position: relative;
		display: flex;
		align-items: center;
		padding: 15px;
		margin: 15px;
		font-size: #0c3854;
		font-size: 14px;
		box-shadow: -1px 1px 5px 1px rgba(0,0,0,0.1), -1px 2px 1px 0 rgba(255,255,255) inset;
		border-radius: 10px;
		background-color: #cfebfd;
		overflow: hidden;
	}
	.todo-list::after {
		content: '';
		position: absolute;
		top: 0;
		bottom: 0;
		left: 0;
		width: 5px;
		background-color: #91d1e8;
	}
	/* checkbox样式 */
	.todo-list__checkbox {
		padding-right: 15px;
	}
	.checkbox {
		width: 20px;
		height: 20px;
		border-radius: 50%;
		background-color: #FFFFFF;
		box-shadow: 0 0 5px 1px rgba(0,0,0,0.1);
	}
	/* 完成待办样式 */
	.todo--finish .checkbox {
		position: relative;
		background-color: #EEEEEE;
	}
	.todo--finish .checkbox:after {
		content: '';
		position: absolute;
		width: 10px;
		height: 10px;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		margin: auto;
		background-color: #CFEBFD;
		border-radius: 50%;
		box-shadow:  0 0 2px 0px rgba(0,0,0,0.2) inset;
	}
	.todo--finish .todo-list__content {
		color: #999999;
	}
	.todo--finish.todo-list:before {
		content: '';
		position: absolute;
		top: 0;
		bottom: 0px;
		left: 40px;
		right: 10px;
		height: 2px;
		margin: auto 0;
		background-color: #bdcdd8;
	}
	.todo--finish.todo-list:after {
		background-color: #CCCCCC;
	}
	/* + 添加待办事项的按钮 */
	.create-todo {
		display: flex;
		justify-content: center;
		align-items: center;
		position: fixed;
		bottom: 20px;
		left: 0;
		right: 0;
		margin: 0 auto;
		width: 50px;
		height: 50px;
		border-radius: 50%;
		background-color: #deeff5;
		box-shadow:  -1px 1px 5px 2px rgba(0,0,0,0.1), -1px 1px 1px 0px rgba(255,255,255) inset;
	}
	.icon-add {
		font-size: 30px;
		color: #add8e6;
	}
	/* 点击创建待办事项弹出内容的样式 */
	.create-content {
		position: fixed;
		bottom: 95px;
		left: 20px;
		right: 20px;
		transition: all 0.3s;
		opacity: 0;
		transform: scale(0) translateY(200%);
	}
	/*动画效果*/
	.create--show {
		opacity: 1;
		transform: scale(1) translateY(0);
	}
	.create-content-box {
		display: flex;
		align-items: center;
		padding: 0px 15px;
		padding-right: 0px;
		border-radius: 50px;
		background-color: #DEEFF5;
		box-shadow: -1px 1px 5px 2px rgba(0,0,0,0.1), -1px 1px 0 rgba(255,255,255) inset;
		z-index: 2;
	}
	.create-input {
		width: 100%;
		padding-right: 15px;
		color: #ADD8E6;
	}
	.create-button {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-shrink: 0;
		width: 80px;
		height: 50px;
		border-radius: 50px;
		font-size: 16px;
		color: #88d4ec;
		box-shadow: -2px 0px 2px 1px rgba(0,0,0,0.1);
	}
	/* 箭头的实现x旋转45° */
	.create-content:after {
		content: '';
		position: absolute;
		right: 0;
		left: 0;
		bottom: -8px;
		margin: 0 auto;
		width: 20px;
		height: 20px;
		background-color: #DEEFF5;
		transform: rotate(45deg);
		box-shadow: 1px 1px 5px 2px rgba(0,0,0,0.1);
		z-index: -1;
	}
	.create-content-box:after {
		content: '';
		position: absolute;
		right: 0;
		left: 0;
		bottom: -8px;
		margin: 0 auto;
		width: 20px;
		height: 20px;
		background-color: #DEEFF5;
		transform: rotate(45deg);
	}
	/* 没有数据的样式 */
	.default {
		padding-top: 100px;
		
	}
	.image-default {
		display: flex;
		justify-content: center;
	}
	.image-default image {
		width: 100%;
	}
	.default-info {
		text-align: center;
		font-size: 14px;
		color: #CCCCCC;
	}
	/*创建的动画效果*/
	.icon-add {
		transition: transform 0.3s;
	}
	.create-todo-active {
		transform:  rotate(135deg);
	}
</style>
